package com.hp.goods_service.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hp.common_api.entity.Category;
import com.hp.goods_service.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    /**
     * 通过父级类型查询子级分类
     * @param pid
     * @return
     */
    @GetMapping("/categories/parent/{pid}")
    public ResponseEntity<List<Category>> getCategoriesByParentId(@PathVariable Long pid){
        List<Category> list = categoryService.list(new QueryWrapper<Category>()
                .lambda().eq(Category::getParentId, pid));
        return ResponseEntity.ok(list);
    }

    /**
     * 通过类型id查询名称
     * @param cid
     * @return
     */
    @GetMapping("category/name/{cid}")
    public ResponseEntity<String> getCategoryNameById(@PathVariable Long cid){
        Category cat = categoryService.getOne(new QueryWrapper<Category>().lambda()
                .eq(Category::getId, cid));
        return ResponseEntity.ok(cat.getName());
    }

    /**
     * 通过类型id查询类型
     * @param cid
     * @return
     */
    @GetMapping("category/{cid}")
    public ResponseEntity<Category> getCategoryById(@PathVariable Long cid){
        Category cat = categoryService.getOne(new QueryWrapper<Category>().lambda()
                .eq(Category::getId, cid));
        return ResponseEntity.ok(cat);
    }
}
